package book;

import java.util.Arrays;

public class CartSolution {
    // 求总重量不超过c的装载最大数量
    int maxLoading(int[] w, int c) {
        Arrays.sort(w);
        int n = 0, l = 0;

        // 贪心选择：当前最轻的物品如果能装得下，则装上货车
        while (n < w.length && l + w[n] <= c) {
            l += w[n];
            n++;
        }
        return n;
    }

    public static void main(String[] args) {
        int[] w = {10, 30, 20, 40, 10};
        int c = 70;
        int n = new CartSolution().maxLoading(w, c);
        System.out.println("最大装载数量为: " + n);
    }

}